#ifndef __EXAMPLE__
#define __EXAMPLE__


#include "coid/coidsvc/coidsvc.h"


//{client
///Example struct used for argument in methods
struct FOO_STRUCT
{
    charstr message;
    int * array;
    int size;

    FOO_STRUCT() : array(NULL), size(0) {}

    FOO_STRUCT & operator = ( const FOO_STRUCT & x ) {
        message = x.message; size = x.size;
        if( array ) {delete array; array = NULL;}
        if( size ) {
            array = new int[size];
            memcpy( array, x.array, size * sizeof(int *) );
        }
        return *this;
    }
    
    ///write object to stream
    friend binstream & operator << ( binstream & b, const FOO_STRUCT & x ) {
        b << x.message << x.size;
        if( x.size ) b.write_raw( x.array, x.size * sizeof(int *) );
        return b;
    }
    
    ///read object from stream
    friend binstream & operator >> ( binstream & b, FOO_STRUCT & x ) {
        b >> x.message >> x.size;
        if( x.array ) {delete x.array; x.array = NULL;}
        if( x.size ) {
            x.array = new int[x.size];
            b.read_raw( x.array, x.size * sizeof(int *) );
        }
        return b;
    }
};
//}client



///Structure for remapping example, server side
struct REMAP_EXAMPLE
{
    const char * err_text;
    REMAP_EXAMPLE() : err_text(NULL) {}
    friend binstream & operator << ( binstream & b, const REMAP_EXAMPLE & x ) {
        return b << x.err_text;
    }
};


/*{client_only
///Structure for remapping example, client side
struct REMAP_EXAMPLE_clientside
{
    charstr err_text;
    friend binstream & operator >> ( binstream & b, REMAP_EXAMPLE_clientside & x ) {
        x.err_text.reset();
        int tmp;
        b >> tmp;
        if( tmp ) {
            char * str = x.err_text.get_append_buf( tmp+1 );
            b.read_raw( str, tmp );
            str[tmp] = 0;
        }
        return b;
    }
};
}client_only
*/




///Example decorated class
class example
{
    cmd_interface * _cmdin;
    FOO_STRUCT      _fs;

public:
    example() : _cmdin(NULL)  {}


    //some optional special functions, recognized by coidgen tool
    net_fnc_s opcd accept_startup_params( cmd_interface & cmd ) {_cmdin = &cmd; return 0;}
    net_fnc_s opcd accept_connect( const charstr & name, net_nolog const password & pwd ) {return 0;}


    //user functions
    net_fnc net_nolog opcd read1( net_out   FOO_STRUCT  & list )    {list =  _fs; return 0;}
    net_fnc net_nolog opcd read2( net_fetch FOO_STRUCT *& list )    {list = &_fs; return 0;}

    net_fnc net_nolog opcd send( net_inout int & ref, const charstr & command, net_out binstreambuf & reply ) const
    {
        return 0;
    }

    net_fnc opcd remap_fnc( net_out net_remap(REMAP_EXAMPLE_clientside &) REMAP_EXAMPLE & arg ) {
        arg.err_text = "Foo";
        return 0;
    }

    //method available only in direct (client&server in the same process) mode
    net_fnc_d opcd get_ptr( FOO_STRUCT ** f )   {*f = &_fs; return 0;}
};


#endif // !__EXAMPLE__
